package jgl.vajra.operator.widget.push;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import jgl.vajra.operator.widget.bean.PushBean;
import jgl.vajra.operator.widget.manager.SPManager;

import com.orhanobut.logger.Logger;

import androidx.fragment.app.FragmentActivity;


public class PushManager {
    /**
     * 判断是否点击了推送通知栏消息
     */
    private final String IS_CLICK_PUSH_MSG = "isClickPushMsg";
    /**
     * 推送消息
     */
    public final String PUSH_MSG = "linkValue";
    /**
     * 推送类型
     */
    public final String PUSH_TYPE = "linkType";
    /**
     * 推送数据
     */
    public final String PUSH_DATA = "pushData";
    /**
     * LAUNCHER 页面 Class
     */
    private Class LAUNCHER_CLASS = null;

    private static class PushManagerSingleton {
        private static final PushManager INSTANCE = new PushManager();

    }

    public static PushManager getInstance() {
        return PushManager.PushManagerSingleton.INSTANCE;
    }

    private PushManager() {

    }

    /**
     * 处理推送操作
     *
     * @param fragmentActivity
     * @param intent           获取传递的数据
     */
    public void handlerPushOp(FragmentActivity fragmentActivity, Intent intent) {
        if (LAUNCHER_CLASS == null) {
            return;
        }
        // 推送消息
        String pushMsg = null;
        // 推送类型
        String pushType = null;
        // 判读是否存在数据
        if (intent != null) {
            Bundle extras = intent.getExtras();
            if (extras == null) return;
            pushType = extras.getString("linkType");
            pushMsg = extras.getString("linkValue");

            SPManager.PushData.savePushData(pushMsg, pushType);
            Logger.e("pushManager  is pushData save ");

        }


        // 关闭当前页面
        fragmentActivity.finish();
        // =================  进行跳转  =================
        Intent pIntent = new Intent(Intent.ACTION_MAIN);
        pIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        pIntent.setComponent(new ComponentName(fragmentActivity.getPackageName(), LAUNCHER_CLASS.getCanonicalName()));
        pIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
        fragmentActivity.startActivity(pIntent);
        // ===============================================
    }

    /**
     * 检查推送操作
     *
     * @param activity
     */
    private void checkPushHandler(FragmentActivity activity) {
        if (activity != null) {
            try {
                // 判断是否点击推送消息 - 必须点击了才进行处理
                PushBean pushData = SPManager.PushData.getPushData();
                if (pushData != null) {
                    // 获取推送消息
                    String pushMsg = pushData.pushData;
                    // 获取推送类型
                    String pushType = pushData.pushType;
                    // 清空全部
                    SPManager.PushData.clearPushData();
                    Logger.e("pushManager  is pushData set ");
                    // 判断是否处理
                    if (pushHandler != null) {
                        pushHandler.onPushHandler(activity, pushMsg, pushType);
                    }
                } else {
                    Logger.e("pushManager  is pushData null ");
                }
            } catch (Exception e) {
                Logger.e("pushManager" + e.getMessage());
            }
        }
    }

    // == 对外提供方法 ==

    /**
     * 检查推送
     *
     * @param activity
     * @param value
     */
    public void checkPush(FragmentActivity activity, String value) {
        if (pushHandler != null) {
            // 判断是否处理推送
            if (pushHandler.isHandlerPush(value)) {
                // 检查推送处理
                checkPushHandler(activity);
            }
        }
    }

    /**
     * 获取跳转Intent
     *
     * @param context
     * @param pushMsg  推送消息
     * @param pushType 推送类型
     * @return
     */
    public Intent getPushHandlerActivityIntent(Context context, String pushMsg, String pushType) {
        try {
            Intent intent = new Intent(context, PushHandlerActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            Bundle bundle = new Bundle();

            bundle.putString(PUSH_MSG, pushMsg);
            bundle.putString(PUSH_TYPE, pushType);
            intent.putExtras(bundle);
            return intent;
        } catch (Exception e) {
            Logger.e("pushManager" + e.getMessage());
        }
        return null;
    }


    /**
     * 设置 android.intent.category.LAUNCHER 类
     *
     * @param launcherClass
     */
    public void setLauncherClass(Class launcherClass) {
        LAUNCHER_CLASS = launcherClass;
    }

    // == 接口 ==

    // 推送判断处理接口
    private PushHandlerListener pushHandler;

    /**
     * 设置推送判断处理接口
     *
     * @param pushHandlers
     */
    public void setPushHandlerListener(PushHandlerListener pushHandlers) {
        pushHandler = pushHandlers;
    }

    /**
     * detail: 推送处理接口
     * Created by Ttt
     */
    public interface PushHandlerListener {

        /**
         * 是否处理推送
         *
         * @param value
         * @return
         */
        boolean isHandlerPush(String value);

        /**
         * 推送处理方法
         *
         * @param activity
         * @param pushData 推送数据
         * @param pushType 推送类型
         */
        void onPushHandler(FragmentActivity activity, String pushData, String pushType);
    }

}
